//
//  JGHomeViewCell.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/13.
//  Copyright © 2016年 sher. All rights reserved.
//

import UIKit
import SDWebImage

fileprivate let degeMagin : CGFloat = 15
fileprivate let itemMagin : CGFloat = 10

class JGHomeViewCell: UITableViewCell {
    // MARK:- 控件属性
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var verifiedView: UIImageView!
    @IBOutlet weak var screenNameLabel: UILabel!
    @IBOutlet weak var vipView: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var picView: JGPicCollectionView!
    @IBOutlet weak var retweetedContentLabel: UILabel!
    @IBOutlet weak var retweetBGView: UIView!
    @IBOutlet weak var bottomToolView: UIView!
    
    // MARK:- 约束的属性
    @IBOutlet weak var contentLabelWCons: NSLayoutConstraint!
    @IBOutlet weak var picViewHCons: NSLayoutConstraint!
    @IBOutlet weak var picViewWCons: NSLayoutConstraint!
    @IBOutlet weak var picViewBottomCons: NSLayoutConstraint!
    @IBOutlet weak var retweetedContentLabelTopCons: NSLayoutConstraint!
    // MARK:- 定义属性
    var viewModel : StatusViewModel? {
        didSet{
            
            guard let viewModel = viewModel else{
                return
            }
            
            //给控件赋值
            iconView.sd_setImage(with: viewModel.profileURL!, placeholderImage: UIImage.init(named: "avatar_default_small"))
            
            verifiedView.image = viewModel.verifiedImage
            
            screenNameLabel.text = viewModel.status?.user?.screen_name
            
            vipView.image = viewModel.vipImage
            
            timeLabel.text = viewModel.createAtText
            
            sourceLabel.text = viewModel.sourceText
            
            contentLabel.text = viewModel.status?.text
            
            //设置名称的文字颜色
            screenNameLabel.textColor = viewModel.vipImage == nil ? UIColor.black : UIColor.orange
            
            //计算picView的宽度和高度
            let picViewSize = calculatePicViewSize(count: viewModel.picURLs.count)
            
            picViewWCons.constant = picViewSize.width
            picViewHCons.constant = picViewSize.height
            
            //将图片的数据传给picview
            picView.picURLs = viewModel.picURLs
            
            //转发正文
            if viewModel.status?.retweeted_status != nil{
                if let screenName = viewModel.status?.retweeted_status?.user?.screen_name, let retweetedText = viewModel.status?.retweeted_status?.text{
                    
                    retweetedContentLabel.text = "@" + "\(screenName):" + retweetedText
                    
                    self.retweetBGView.isHidden = false
                    
                    self.retweetedContentLabelTopCons.constant = 15
                }
            }else{
                retweetedContentLabel.text = nil
                
                self.retweetBGView.isHidden = true
                
                self.retweetedContentLabelTopCons.constant = 0
            }
            //计算cell高度
            if viewModel.cellHeight == 0{
                self.layoutIfNeeded()
                //获取底部工具栏最大的Y值(3.0语法)
                viewModel.cellHeight = self.bottomToolView.frame.maxY
            }
            
        }
    }
    
    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        
        //设置微博的宽度
        contentLabelWCons.constant = UIScreen.main.bounds.width - degeMagin * 2
        
    }

}

// MARK:- 计算方法
extension JGHomeViewCell{
    fileprivate func calculatePicViewSize(count : Int) -> CGSize{
        self.picView.layoutIfNeeded()
        //没有配图
        if count == 0{
            picViewBottomCons.constant = 0
            return CGSize.zero
        }
        
        picViewBottomCons.constant = 10
        
        //取出picView对应的layout
        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
        //单张图片
        if count == 1{
            //取出单张缓存图片
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewModel?.picURLs.first?.absoluteString)
            
            layout.itemSize = CGSize.init(width: (image?.size.width)! * 2, height: (image?.size.height)! * 2)
            
            return CGSize.init(width: (image?.size.width)! * 2, height: (image?.size.height)! * 2)
        }
        //计算imageview的 W 和 H
        let imageViewWH = (UIScreen.main.bounds.width - 2 * degeMagin - 2 * itemMagin) / 3
        layout.itemSize = CGSize.init(width: imageViewWH, height: imageViewWH)
        //四张配图
        if count == 4{
            let picViewWH = imageViewWH * 2 + itemMagin
            return CGSize.init(width: picViewWH, height: picViewWH)
        }
        
        //其他张配图
        //计算行数
        let rows = CGFloat((count - 1) / 3 + 1)
        //计算picview高度
        let picViewH = rows * imageViewWH + (rows - 1) * itemMagin
        //计算picview宽度
        let picViewW = UIScreen.main.bounds.width - 2 * degeMagin
        return CGSize.init(width: picViewW, height: picViewH)
        
    }
}












